package com.example.dilidili_ting.view;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.dilidili_ting.R;
import com.example.dilidili_ting.activity.loginActivityTing;
import com.example.dilidili_ting.adapter.LikeAdapterTing;
import com.example.dilidili_ting.bean.DataBeanTing;
import com.example.dilidili_ting.bean.LikeBeanTing;
import com.example.dilidili_ting.bean.MovieBeanTing;
import com.example.dilidili_ting.utils.OkhttpUtilsTing;
import com.example.dilidili_ting.utils.SharedPreferencesUtilsTing;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.ryan.rv_gallery.AnimManager;
import com.ryan.rv_gallery.GalleryRecyclerView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class LikeViewTing {

    private RelativeLayout rlContainer;
    private RelativeLayout rlScWuTing;
    private GalleryRecyclerView rvScListTing;
    private TextView tvScWuTing;
    private Activity mContextTing;
    private LayoutInflater mInflaterTing;
    private View mCurrentViewTing;
    private LikeAdapterTing likeAdapterTing;
    private List<MovieBeanTing> listMovieTing;
    private DataBeanTing<MovieBeanTing> r;

    public LikeViewTing(Activity context){
        mContextTing = context;
        mInflaterTing = LayoutInflater.from(mContextTing);
    }

//    创建view界面
    private void createView(){
        initView();
    }

//    获取收藏接口的数据
    public void setData() {
        JsonObject jo = new JsonObject();
        jo.addProperty("userId", SharedPreferencesUtilsTing.readLoginUserID(mContextTing));
        OkhttpUtilsTing.isPOST("favorite/getFavoriteByUserId", true, jo, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {}

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res = response.body().string();
                Gson gson = new Gson();
                final DataBeanTing<List<LikeBeanTing>> r = gson.fromJson(res, new TypeToken<DataBeanTing<List<LikeBeanTing>>>(){}.getType());
//                System.out.println(res);
                listMovieTing = new ArrayList<>();
                mContextTing.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(r.data.size() == 0){
                            rvScListTing.setVisibility(View.GONE);
                            rvScListTing.setAdapter(null);
                            rlScWuTing.setVisibility(View.VISIBLE);
                            tvScWuTing.setText("暂无收藏数据");
                        }else {
                            for (int i=0;i<r.data.size();i++){
                                getMovieId(r.data.get(i).getMovieId(),r.data,i+1);
                            }
                        }
                    }
                });
            }
        });
    }

//    根据id查找电影详情
    private void getMovieId(Integer id,List<LikeBeanTing> data,Integer i){
//        System.out.println("ii"+id);
        JsonObject jo = new JsonObject();
        jo.addProperty("movieId",id);
        OkhttpUtilsTing.isPOST("movie/getMovieById", true, jo, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {}

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res = response.body().string();
//                System.out.println("我是后面的"+res);
                Gson gson = new Gson();
                r = gson.fromJson(res, new TypeToken<DataBeanTing<MovieBeanTing>>(){}.getType());
                listMovieTing.add(r.data);
                mContextTing.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
//                        System.out.println("i"+i);
//                        System.out.println("data.size"+data.size());
                        if(i.equals(data.size())){
//                            System.out.println("r.data"+new Gson().toJson(listMovieTing));
                            likeAdapterTing = new LikeAdapterTing(mContextTing, listMovieTing);
                            rvScListTing.setAdapter(likeAdapterTing);
                            likeAdapterTing.notifyDataSetChanged();
                            rvScListTing.initFlingSpeed(9000)
                                    .initPageParams(0, 70)
                                    .setAnimFactor(0.1f)
                                    .setAnimType(AnimManager.ANIM_BOTTOM_TO_TOP)
                                    .autoPlay(false)
                                    .intervalTime(2000)
                                    .initPosition(0).setUp();

                        }
                    }
                });
            }
        });
    }

//    初始化控件
    public void initView() {
        mCurrentViewTing = mInflaterTing.inflate(R.layout.view_like_ting,null);
        rlContainer = (RelativeLayout) mCurrentViewTing.findViewById(R.id.rl_container);
        rvScListTing = (GalleryRecyclerView) mCurrentViewTing.findViewById(R.id.rv_sc_list_ting);
        rvScListTing.setLayoutManager(new LinearLayoutManager(mContextTing, LinearLayoutManager.HORIZONTAL, false));// 设置 recyclerview 布局方式为横向布局
        tvScWuTing = (TextView) mCurrentViewTing.findViewById(R.id.tv_sc_wu_ting);
        rlScWuTing = (RelativeLayout) mCurrentViewTing.findViewById(R.id.rl_sc_wu_ting);
//        loginStatus();
    }
//        判断是否登录
    public void loginStatus(){
        if (SharedPreferencesUtilsTing.readLoginStatus(mContextTing)){
            rlScWuTing.setVisibility(View.GONE);
            rvScListTing.setVisibility(View.VISIBLE);
            setData();
        }else {
            rvScListTing.setVisibility(View.GONE);
            rlScWuTing.setVisibility(View.VISIBLE);
            tvScWuTing.setText("请先登录");
//            跳转到登录页面
            tvScWuTing.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(mContextTing,  loginActivityTing.class);
                    mContextTing.startActivityForResult(intent,1);
                }
            });
        }
    }

    public View getView(){
        //判断当前view是否存在，不存在则创建view
        if (mCurrentViewTing == null){
            createView(); //创建view
        }
        return mCurrentViewTing; //返回当前view对象
    }

//     显示当前view界面
    public void showView(){
        //
        if (mCurrentViewTing == null){
            createView();
        }
        mCurrentViewTing.setVisibility(View.VISIBLE);  //显示view
    }


}
